package com.snake.win.obj;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.snake.win.AI.SnakeAi;
import com.snake.win.utils.GameUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public  class Snake {
    public int score=0;

    public int c=0;
    int i =0;

    public HeadObj headObj;
    public ArrayList<GameObj> bodyObjList;

    private HashSet<String> map;

    public FoodObj foodObj;

    public GameObj tailobj;

    public enum Direction {
        right, left, up, down,no
    }

    Direction direction = Direction.right;
    Direction newDirection=Direction.right;
    public Direction AIDirection=Direction.right;
    public boolean canAI=true;

    boolean allowMove = true;

    public SnakeAi snakeAi=new SnakeAi();

    public void updataMap(){
        synchronized (map) {
            map.clear();//先移除旧的站位点
            map.add(headObj.toString());
            for (GameObj b : bodyObjList) {
                map.add(b.toString());
            }
        }
    }

    public void update(SpriteBatch g) {
        allowMove=this.headObj.moveOK;
        for(GameObj bodyObj: bodyObjList){
            if (!bodyObj.moveOK) {
                allowMove = false;
                break;
            }
        }
        if(allowMove){
            canAI=false;
                //newDirection=snakeAi.play2((this));
            if(AIDirection!=Direction.no){
                direction=AIDirection;
                move(direction);
            }
            else
            {
                Gdx.app.error("HeadObj", "no direction");
            }

            switch (direction){
                case right:headObj.sprite.setTexture(GameUtils.rightImg);break;
                case left:headObj.sprite.setTexture(GameUtils.leftImg);break;
                case up:headObj.sprite.setTexture(GameUtils.upImg);break;
                case down:headObj.sprite.setTexture(GameUtils.downImg);break;
            }
            canAI=true;
        }

        //moveCounter = 0; // 重置计数器
        //}
        // move结束后，新的body添加到原来的body后

        // 越界处理
        if (headObj.snake_x < 1) headObj.snake_x = 20;
        else if (headObj.snake_x > 20) headObj.snake_x = 1;
        else if (headObj.snake_y < 1) headObj.snake_y = 20;
        else if (headObj.snake_y > 20) headObj.snake_y = 1;
    }

    //控制移动方向
    public void ChangeDirection() {
        if (Gdx.input.isKeyPressed(Input.Keys.A)) {
            if (!(direction == Direction.right)) {
                newDirection = Direction.left;
                //sprite.setTexture(GameUtils.leftImg);
            }
        } else if (Gdx.input.isKeyPressed(Input.Keys.D)) {
            if (!(direction == Direction.left)) {
                newDirection = Direction.right;
                //sprite.setTexture(GameUtils.rightImg);
            }
        } else if (Gdx.input.isKeyPressed(Input.Keys.W)) {
            if (!(direction == Direction.down)) {
                newDirection = Direction.up;
                //sprite.setTexture(GameUtils.upImg);
            }
        } else if (Gdx.input.isKeyPressed(Input.Keys.S)) {
            if (!(direction == Direction.up)) {
                newDirection = Direction.down;
                //sprite.setTexture(GameUtils.downImg);
            }
        }
    }

    public void move(Direction direction) {
        if(!headObj.toString().equals(foodObj.toString())) {
            this.tailobj.snake_x = this.bodyObjList.get(this.bodyObjList.size() - 1).snake_x;
            this.tailobj.snake_y = this.bodyObjList.get(this.bodyObjList.size() - 1).snake_y;
        }
        else{
            this.bodyObjList.add(new BodyObj(GameUtils.bodyImg, tailobj.snake_x, tailobj.snake_y));
            foodObj=new FoodObj().getFood(this);
        }

        //蛇身的移动
        ArrayList<GameObj> bodyObjList = this.bodyObjList;
        for (int i = bodyObjList.size() - 1; i >= 1; i--) {
            bodyObjList.get(i).snake_x = bodyObjList.get(i - 1).snake_x;
            bodyObjList.get(i).snake_y = bodyObjList.get(i - 1).snake_y;
            //蛇头与身体的碰撞
//            if (this.snake_x == bodyObjList.get(i).snake_x && this.snake_y == bodyObjList.get(i).snake_y) {
//                //失败
//                objManager.state = ObjManager.State.DIE;
//            }
        }
        bodyObjList.get(0).snake_x = this.headObj.snake_x;
        bodyObjList.get(0).snake_y = this.headObj.snake_y;

        //蛇头的移动
        switch (direction) {
            case up:
                headObj.snake_y += 1;
                break;
            case down:
                headObj.snake_y -= 1;
                break;
            case left:
                headObj.snake_x -= 1;
                break;
            case right:
                headObj.snake_x += 1;
                break;
            default:
                break;
        }

        this.updataMap();
    }

    public HashSet<String> getMap() {
        return map;
    }
    public void setMap(HashSet<String> map) {
        this.map = map;
    }
    public Set<String> getSafeCopy() {
        synchronized (map) {
            return new HashSet<String>(map);
        }
    }
    public void addString(String item) {
        synchronized (map) {
            map.add(item);
        }
    }
}
